package com.cwz.send.utils;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: w-demo
 * @description: 邮件工具类
 * @author: Wen
 **/
public class MailUtils {
	/**
	 * @Description: 简单文本邮件
	 * @Author: Wen
	 * @param mailSender:	邮箱发送实体类（自动注入）
	 * @param from:			发送人
	 * @param to:			接收者
	 * @param subject:		邮件主题
	 * @param content:		邮件内容
	 * @return: void
	 **/
	public static void sendSimpleMail(JavaMailSender mailSender, String from, String to, String subject, String content) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(from);
		message.setTo(to);
		message.setSubject(subject);
		message.setText(content);
		mailSender.send(message);
	}

	/**
	 * @Description: 文本邮件
	 * @Author: Wen
	 * @param mailSender:	邮箱发送实体类（自动注入）
	 * @param from:			发送人
	 * @param to:			接收者
	 * @param subject:		邮件主题
	 * @param content:		邮件内容
	 * @return: void
	 **/
	public static void sendHtmlMail (JavaMailSender mailSender, String from, String to, String subject, String content) throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		mailSender.send(message);
	}

	/**
	 * @Description: 附件邮件
	 * @Author: Wen
	 * @param mailSender:	邮箱发送实体类（自动注入）
	 * @param from:			发送人
	 * @param to:			接收者
	 * @param subject:		邮件主题
	 * @param content:		邮件内容
	 * @param filePath:		附件路径
	 * @return: void
	 **/
	public static void sendAttachmentsMail (JavaMailSender mailSender, String from, String to, String subject, String content, String filePath) throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		FileSystemResource file = new FileSystemResource(new File(filePath));
		helper.addAttachment(file.getFilename(), file);
		mailSender.send(message);
	}

	/**
	 * @Description: 图片邮件
	 * @Author: Wen
	 * @param mailSender:	邮箱发送实体类（自动注入）
	 * @param from:			发送人
	 * @param to:			接收者
	 * @param subject:		邮件主题
	 * @param content:		邮件内容
	 * @param rscPath:		图片路径
	 * @param rscId:		图片ID
	 * @return: void
	 **/
	public static void sendInlinkResourceMail (JavaMailSender mailSender, String from, String to, String subject, String content, String rscPath, String rscId) throws MessagingException {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true);
		helper.setFrom(from);
		helper.setTo(to);
		helper.setSubject(subject);
		helper.setText(content, true);
		FileSystemResource file = new FileSystemResource(new File(rscPath));
		helper.addInline(rscId, file);
		mailSender.send(message);
	}

	/**
	 * @Description: 读取自定义 HTML 模板内容
	 * @Author: Wen
	 * @param filePath:		自定义 HTML 模板路径
	 * @return: java.lang.String
	 **/
	public static String readFile(String filePath) throws IOException {
		StringBuffer sb = new StringBuffer();
		MailUtils.readToBuffer(sb, filePath);
		return sb.toString();
	}

	/**
	 * @Description: 解析自定义 HTML 模板代码，替换参数
	 * @Author: Wen
	 * @param template:		读取的自定义 HTML 模板内容
	 * @param params:		替换参数
	 * @return: java.lang.String
	 **/
	public static String processTemplate(String template, Map params) {
		StringBuffer sb = new StringBuffer();
		Matcher matcher = Pattern.compile("\\$\\{\\w+\\}").matcher(template);
		while (matcher.find()) {
			String param = matcher.group();
			Object value = params.get(param.substring(2, param.length() - 1));
			matcher.appendReplacement(sb, value == null ? "" : value.toString());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	/**
	 * @Description: 将自定义 HTML 模板内容读入到 buffer 中
	 * @Author: Wen
	 * @param buffer:		缓冲池
	 * @param filePath:		自定义 HTML 模板路径
	 * @return: void
	 **/
	public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
		InputStream is = new ClassPathResource(filePath).getInputStream();
		// 用来保存每行读取的内容
		String line;
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		// 读取第一行
		line = reader.readLine();
		// 如果 line 为空说明读完了
		while (line != null) {
			// 将读到的内容添加到 buffer 中
			buffer.append(line);
			// 添加换行符
			buffer.append("\n");
			// 读取下一行
			line = reader.readLine();
		}
		reader.close();
		is.close();
	}
}
